package com.mew.slots.core.game.hdcz.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.entity.game.WinFree;
import com.mew.slots.core.common.game.BaseGameService;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.common.messge.response.SlotsPlayResponseMessage;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.hdcz.bo.HdczInRoom;
import com.mew.slots.core.game.hdcz.message.HdczResponseMessage;
import com.mew.slots.core.game.hdcz.roller.HdczIli;
import com.mew.slots.core.game.hdcz.roller.HdczXml;
import com.mew.slots.core.game.hdcz.room.HdczRoom;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author Dylin
 */
@Service
@Slf4j
public class HdczService extends BaseGameService {
    @Override
    public BasePlayMsgs startGame(StrategyGameVO player) {
        HdczInRoom in = new HdczInRoom();
        if (StringUtils.isNotEmpty(player.getWinFree().getJson())){
            try {
                in = JSON.parseObject(player.getWinFree().getJson(), HdczInRoom.class);
            }catch (Exception e){
                log.debug("WinFree json解析出错：{}",player.getWinFree().getJson());
            }
        }
        HdczRoom room = new HdczRoom(in,player.getWinFree().getBounsRate());
        room.init(player.getRollerId(), HdczIli.getInstance(),in,player.getWinFree().getFrees()>0,player.getInBetNumber(),player.getControlType());
        room.roundPlayIli();
        player.getWinFree().setJson(JSON.toJSONString(in));
        return room.getRes();
    }

    @Override
    public SlotsPlayResponseMessage myResp(WinFree winFree, BasePlayMsgs res) {
        HdczResponseMessage message = new HdczResponseMessage();

        if(res.isChange()){
            //下局是消除
            if(winFree.getBounsRate()<3){
                winFree.setBounsRate(winFree.getBounsRate()+1);
            }
        }else{
            winFree.setBounsRate(0);
        }
        message.setChange(res.isChange()?1:0);
        message.setChangeRate(winFree.getBounsRate());
        return message;
    }
    @Override
    public void myAfterResp(SlotsPlayResponseMessage message,StrategyGameVO player, BasePlayMsgs res) {}
}
